package com.third.customer.model.dao.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.third.customer.model.dao.CompanyDao;
import com.third.customer.model.dao.param.CompanyParam;
import com.third.customer.model.mapper.CompanyMapper;
import com.third.customer.model.tables.Company;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.Optional;

/**
 * Created by hehuaichun on 2020/5/21.
 */
@Repository
public class CompanyDaoImpl implements CompanyDao {

    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public Page<Company> query(CompanyParam param) {
        Page<Company> page = new Page<>(param.getPage(), param.getLimit());
        return new LambdaQueryChainWrapper<>(companyMapper)
                .like(!StringUtils.isEmpty(param.getName()), Company::getName, param.getName())
                .page(page);
    }

    @Override
    public Optional<Company> getById(long id) {
        return Optional.ofNullable(companyMapper.selectById(id));
    }
}
